package com.gip.config;

import com.gip.utils.AuctionPriceException;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class MyExceptionHandler implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {

        AuctionPriceException ex = null;

       // 判断是否是用户自定义异常
        if (e instanceof AuctionPriceException) {
            ex = (AuctionPriceException) e;
        } else {
            ex = new AuctionPriceException("系统异常");
            System.out.println(e);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("msg",ex.getMessage());
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
